<template>
  <div id="detail">
    <!-- 顶部导航栏 -->
    <detail-nav-bar @navClick="navBarClick"
                    :current-index="currentIndex"
                    class="detail-nav"
                    ref="nav"></detail-nav-bar>
    <scroll class="content"
            ref='scroll'
            :probe-type="3"
            @scrollProbe="contentScroll">
      <!-- 主图轮播图 -->
      <detail-swiper :top-images="topImages"></detail-swiper>
      <!-- 基本信息 -->
      <detail-base-info ref="base"
                        :goods='goods'></detail-base-info>
      <!-- 店铺信息 -->
      <detail-shop-info :shop='shop'></detail-shop-info>
      <!-- 商品信息 -->
      <detail-goods-info :detail-info="detailInfo"
                         @imageLoad="imageLoad"></detail-goods-info>
      <!-- 商品参数信息 -->
      <detail-param-info ref="param"
                         :param-info="paramInfo"></detail-param-info>
      <!-- 评论信息 -->
      <detail-comment-info ref="comment"
                           :commentInfo="commentInfo"></detail-comment-info>
      <!-- 推荐商品 -->
      <detail-recommend-info ref="recommend" :recommend-list="recommendList"></detail-recommend-info>
    </scroll>
    <!-- 返回顶部 -->
    <back-top @click.native="backTopClick"
              v-show="isShowBackTop" />
    <!-- 底部按钮 -->
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import DetailBottomBar from './childComps/DetailBottomBar'
import DetailRecommendInfo from './childComps/DetailRecommendInfo'

import Scroll from 'components/common/scroll/Scroll'
import BackTop from 'components/content/backTop/BackTop'

import { getDetail, getRecommend, Goods, Shop, GoodsParam } from 'network/detail'
import { itemListenerMixin, backTopMixin } from 'common/mixin'
import { mapActions } from 'vuex'

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailRecommendInfo,
    DetailBottomBar,
    Scroll,
    BackTop
  },
  mixins: [itemListenerMixin, backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommendList: [],
      themeTops: [],
      currentIndex: 0
    }
  },
  methods: {
    ...mapActions(['addCart']),
    /**
     * 网络请求事件
     */
    getDetailData() {
      getDetail(this.iid).then(res => {
        const data = res.result
        // 获取顶部轮播图片
        this.topImages = data.itemInfo.topImages
        // 获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
        // 获取店铺信息
        this.shop = new Shop(data.shopInfo)
        // 获取商品的详情数据
        this.detailInfo = data.detailInfo
        // 获取商品参数信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
        // 获取评论信息
        if (data.rate.list) {
          this.commentInfo = data.rate.list[0];
        }
      })
    },
    navBarClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTops[index], 100)
    },
    addToCart() {
      /**
       * 添加购物车
       */
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.iid

      // this.$store.dispatch('addCart', product)
      // console.log('添加购物车成功！')
      this.addCart(product).then(res => {
        // console.log(res)
        this.$toast.show(res)
      })
    },

    getRecommendData() {
      getRecommend().then(res => {
        this.recommendList = res.data.list
      })
    },

    imageLoad() {
      this.$refs.scroll.refresh()

      // 获取各组件的位置
      this._getOffsetTops()
    },
    _getOffsetTops() {
      this.themeTops = []
      this.themeTops.push(0)
      // this.themeTops.push(this.$refs.base.$el.offsetTop)
      this.themeTops.push(this.$refs.param.$el.offsetTop)
      this.themeTops.push(this.$refs.comment.$el.offsetTop)
      this.themeTops.push(this.$refs.recommend.$el.offsetTop)
      this.themeTops.push(Number.MAX_VALUE)
    },
    contentScroll(position) {
      /**
       * 监听内容滚动
       */
      // 当屏幕滚动到一定位置后，显示回到顶部按钮
      this.isShowBackTop = -position.y > 1000
      // 动态高亮nav-bar的title
      this._listenScrollTheme(position)
    },
    _listenScrollTheme(pos) {
      // 监听内容滚动位置，高亮对应nav-bar的title
      let len = this.themeTops.length
      if (len > 0) {
        for (let i = 0; i < len; i++) {
          if (this.currentIndex !== i && (-pos.y >= this.themeTops[i] && -pos.y < this.themeTops[i + 1])) {
            this.currentIndex = i
          }
        }
      }
    }
  },
  created() {
    // 获取详情id
    this.iid = this.$route.params.iid

    // 请求详情数据
    this.getDetailData()

    // 获取推荐商品数据
    this.getRecommendData()
  },
  destroyed() {
    // 暂停监听图片加载
    this.$bus.$off('itemImgLoad', this.itemImgListener)
  }
}
</script>

<style scoped>
#detail {
  height: 100vh;
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.detail-nav {
  position: relative;
}

.content {
  /* height: calc(100% - 44px); */
  position: absolute;
  top: 44px;
  bottom: 58px;
  left: 0;
  right: 0;
}
</style>
